/*
 * @Author: szx
 * @Date: 2022-03-07 14:04:29
 * @LastEditTime: 2022-03-07 14:27:29
 * @Description:
 * @FilePath: \leetcode\200-299\236\236_2.js
 */

/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
var lowestCommonAncestor = function (root, p, q) {
    if (root == p || root == q || root == null) return root;
    const left = lowestCommonAncestor(root.left, p, q);
    const right = lowestCommonAncestor(root.right, p, q);
    if (left && right) return root;
    if (left) return left;
    if (right) return right;
};
